﻿using AutoMapper;
using DFGK.IRepository;
using DFGK.IService;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DFGK.Service
{
    public class ActionService : BaseService, IActionService
    {
        private readonly IActionRepository _actionRepository;  
        private readonly IMenuActionRepository _menuActionRepository;
        public ActionService(IMapper mapper, IActionRepository _actionRepository, IMenuActionRepository menuActionRepository) : base(mapper)
        {
            this._actionRepository = _actionRepository;
            _menuActionRepository = menuActionRepository;
        }

        /// <summary>
        /// 获取按钮列表
        /// </summary>
        /// <param name="aname"></param>
        /// <param name="status"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="totalCount"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public List<DFGK_WMS.Model.Models.Action> GetActionList(string aname, int status, int pageIndex, int pageSize, out int totalCount)
        {
            var alist = _actionRepository.GetEntityAlls();

            if (!string.IsNullOrEmpty(aname))
            {
                alist = alist.Where(x => x.ActionName.Contains(aname));
            }

            if (status <= 1)
            {
                alist = alist.Where(x => x.Status == status);
            }
            var alists = _actionRepository.GetEntityAlls(alist,pageIndex,pageSize,out totalCount,x=>x.CreateDate,false);
            return alists.ToList();
        }

        /// <summary>
        /// 左侧穿梭框显示
        /// </summary>
        /// <param name="mid"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public List<DFGK_WMS.Model.Models.Action> GetLeftMenuActionList(long mid)
        {
            var missingButtons = _actionRepository.GetEntityWhereAll(b => !_menuActionRepository.GetEntityAlls().Any(r => r.MenuId == mid && r.ActionId == b.Id))
    .ToList();
            return missingButtons;
        }

        /// <summary>
        /// 右侧穿梭框显示
        /// </summary>
        /// <param name="mid"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public List<DFGK_WMS.Model.Models.Action> GetRightMenuActionList(long mid)
        {
            var mlist = _menuActionRepository.GetEntityAlls();
            var alist = _actionRepository.GetEntityAlls();
            var list = (from a in mlist
                        join b in alist
                        on a.ActionId equals b.Id
                        where a.MenuId == mid
                        select b).Distinct();
            return list.ToList();
        }
    }
}
